package com.liu.stockmarket.ui.stock.modify

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.os.bundleOf
import androidx.lifecycle.ViewModelProvider
import com.liu.stockmarket.BaseActivity
import com.liu.stockmarket.StockMarketApplication
import com.liu.stockmarket.databinding.ActivityModifyStockBinding
import com.liu.stockmarket.ui.search.SearchResultAdapter
import com.liu.stockmarket.ui.stock.add.StockAddActivity
import com.liu.stockmarket.utils.getString
import com.liu.stockmarket.utils.start
import com.liu.stockmarket.utils.toast
import java.math.BigDecimal

class StockModifyActivity : BaseActivity() {

    private lateinit var binging: ActivityModifyStockBinding

    private lateinit var viewModel: StockModifyViewModel

    private var stockId:Long?=null

    override fun initView(savedInstanceState: Bundle?) {
        binging = ActivityModifyStockBinding.inflate(layoutInflater)

        setContentView(binging.root)
        setToolbar(binging.toolbar.toolbar)


        val stockMarketApplication = this.application as StockMarketApplication

        viewModel = ViewModelProvider(
            this,
            StockModifyViewModel.StockModifyViewModelFactory(stockMarketApplication.stockRepository)
        ).get(StockModifyViewModel::class.java)

        intent.extras?.let {
            stockId = it.getLong(STOCK_ID_KEY)
            if (stockId == null) {
                this.toast("股票不存在")
                finish()
            } else {
                title = it.getString(STOCK_NAME_KEY,"")
                setSubTitle(it.getString(STOCK_CODE_KEY,""))
                binging.etHoldingPrice.setText((it.getSerializable(STOCK_PRICE_KEY) as BigDecimal).getString(4))
                binging.etHoldingNumber.setText((it.getSerializable(STOCK_NUMBER_KEY) as BigDecimal).getString(4))
            }
        }
    }

    override fun listener() {
        binging.btnSave.setOnClickListener {
            doSave()
        }
    }

    private fun doSave() {
        val holdingPrice = binging.etHoldingPrice.getString()
        val holdingNumber = binging.etHoldingNumber.getString()

        if (holdingPrice.isEmpty()) {
            binging.etHoldingPrice.error = "请输入持仓成本价"
            return
        }
        if (holdingNumber.isEmpty()) {
            binging.etHoldingNumber.error = "请输入持仓数量"
            return
        }

        viewModel.save(stockId!!, BigDecimal(holdingPrice), BigDecimal(holdingNumber))
    }

    override fun observer() {
        viewModel.result.observe(this) {
            this.toast("修改成功")
            finish()
        }
    }

    companion object {
        const val STOCK_ID_KEY = "stockId"
        const val STOCK_NAME_KEY = "stockName"
        const val STOCK_CODE_KEY = "stockCode"
        const val STOCK_PRICE_KEY = "stockPrice"
        const val STOCK_NUMBER_KEY = "stockNumber"
        fun start(
            context: Context,
            stockId: Long,
            stockName: String,
            stockCode: String,
            holdingPrice: BigDecimal,
            holdingNumber: BigDecimal
        ) {
            context.start(
                bundleOf(
                    STOCK_ID_KEY to stockId,
                    STOCK_NAME_KEY to stockName,
                    STOCK_CODE_KEY to stockCode,
                    STOCK_PRICE_KEY to holdingPrice,
                    STOCK_NUMBER_KEY to holdingNumber,
                ), StockModifyActivity::class.java
            )
        }
    }

}